package org.anderground.modules.editor.importers.pmz;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.zip.ZipException;

import org.anderground.core.Logger;
import org.anderground.core.model.ModelData;
import org.anderground.modules.editor.importers.ModelImporter;
import org.anderground.modules.editor.importers.pmz.archive.PmzFile;
import org.anderground.modules.editor.importers.pmz.map.PmzMetroMap;

/**
 * @author Ilya Shurigyn
 */
public class PmzModelImporter implements ModelImporter {
	public static boolean MERGE_SIMILAR_STATIONS = false;
	// if lines with the same color is one line(this isn't so in new-york map,
	// for example)
	public static boolean MERGE_SIMILAR_LINES = false;
	public static boolean REMOVE_ZEROSIZE_TUNNELS = false;

	public PmzMetroMap map;

	public PmzModelImporter() {
		PmzMetroMap.MERGE_SIMILAR_LINES = MERGE_SIMILAR_LINES;
		PmzMetroMap.MERGE_SIMILAR_STATIONS = MERGE_SIMILAR_STATIONS;
		PmzMetroMap.REMOVE_ZEROSIZE_TUNNELS = REMOVE_ZEROSIZE_TUNNELS;
	}

	@Override
	public String getFormatDescription() {
		return "PMetro underground map";
	}

	@Override
	public String[] getSupportedExtensions() {
		return new String[] { "pmz" };
	}

	@Override
	public ModelData read(File file) throws FileNotFoundException {

		ModelData.Builder builder = new ModelData.Builder();

		try {
			PmzFile f = new PmzFile(file);
			map = new PmzMetroMap(f);
			map.build(builder);

		} catch (ZipException e) {
			Logger.e("pmz file:" + file.getAbsolutePath() + "corrupted");
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return builder.getModelData();
	}

	@Override
	public boolean canReadFile(File file) {
		String extension = file.getName().substring(file.getName().lastIndexOf('.')+1);
		Logger.i("extenstion:"+ extension);
		if(extension.equals("pmz")) return true;
		return false;
	}

}
